package com.lsh.payment.core.service.async;

import com.google.common.eventbus.AsyncEventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Project Name: lsh-payment
 * @author peter
 * Date: 16/12/22
 * 北京链商电子商务有限公司
 * Package com.lsh.payment.core.service.AsyncService
 * desc:异步处理类
 */
@Component
@Lazy(value = false)
public class PayEventBus {

    private static AsyncEventBus asyncEventBus;

    @Autowired
    private List<IEventListener> eventListeners;

    @PostConstruct
    public void init() {

        asyncEventBus = new AsyncEventBus(new ThreadPoolExecutor(10, 10,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>()));
        for(IEventListener eventListener : eventListeners){
            asyncEventBus.register(eventListener);
        }
    }

    public static void post(Object event){
        asyncEventBus.post(event);
    }
}
